package com.bphx.android.activities;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import com.bphx.android.IStorage;
import com.bphx.android.activities.adapters.WordsManagerListAdapter;
import com.bphx.android.activities.dialogs.InfoWordDialog;
import com.bphx.android.db.Storage;
import com.bphx.android.exception.BaseException;
import com.bphx.android.ui.listeners.IDialogListener;
import com.bphx.android.words.Word;

import java.util.ArrayList;

import static com.bphx.android.R.id.*;
import static com.bphx.android.R.layout.words_manager;
import static com.bphx.android.utils.Constants.*;

/**
 * @author Vyacheslav Kovalyov
 * @version 1.0
 */
public class WordManagerActivity extends Activity implements IDialogListener<Word> {

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(words_manager);

        if (savedInstanceState == null) {

            IStorage storage = new Storage(this);
            try {
                words = storage.loadWords();
            } catch (BaseException ignore) {
            }
        } else {

            words = savedInstanceState.getParcelableArrayList(WORDS);
        }


        if (words == null) {
            words = new ArrayList<Word>();
        }

        initViews();

        if (savedInstanceState != null) {
            if (savedInstanceState.getBoolean(DLG_SHOWED)) {
                showDialog((Word) savedInstanceState.getParcelable(DLG_PARAM));
            }
        }
    }

    private void initViews() {
        adapter = new WordsManagerListAdapter(this, words);

        ListView listView = (ListView) findViewById(words_manager_list);
        listView.setAdapter(adapter);

        findViewById(backButton).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        findViewById(newWordButton).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(WordManagerActivity.this, CreateWordActivity.class);
                startActivityForResult(intent, CREATE_WORD_ACTIVITY);
            }
        });
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {

        outState.putParcelableArrayList(WORDS, words);

        if (infoDialog == null || !infoDialog.isShowing()) {
            outState.putBoolean(DLG_SHOWED, false);
        } else {
            outState.putBoolean(DLG_SHOWED, true);
        }

        outState.putParcelable(DLG_PARAM, infoWord);

        super.onSaveInstanceState(outState);
    }

    @Override
    protected void onDestroy() {
        if (infoDialog != null) {
            infoDialog.cancel();
        }

        super.onDestroy();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == CREATE_WORD_ACTIVITY && resultCode == RESULT_OK) {
            Word word = (Word) data.getExtras().get(WORD_KEY);
            words.add(word);
            adapter.notifyDataSetChanged();
        }

        if (requestCode == EDIT_WORD_ACTIVITY && resultCode == RESULT_OK) {

            Word word = (Word) data.getExtras().get(WORD_KEY);
            Word originalWord = (Word) data.getExtras().get(ORIGINAL_WORD_KEY);

            for (Word w : words) {
                if (w.getOriginalWord().equals(originalWord.getOriginalWord())) {
                    int index = words.indexOf(w);
                    words.remove(w);
                    words.add(index, word);

                    break;
                }
            }

            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public void onCancel() {
        infoWord = null;
        infoDialog = null;
    }

    @Override
    public void showDialog(Word param) {
        infoWord = param;

        if (param != null) {
            infoDialog = new InfoWordDialog(this, param, this);
            infoDialog.show();
        }
    }

    private ArrayList<Word> words;
    private WordsManagerListAdapter adapter;

    //use for saving state only
    private Word infoWord;
    private InfoWordDialog infoDialog;
}